﻿using System;
using System.Web;

namespace MvcSecurityLogging
{
    public static class Log
    {
        #region Membership

        public static void Membership(string message)
        {
            var ex = new MembershipException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CreateUser(string userName)
        {
            var ex = new CreateUserException("User created. UserName: " + userName);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorCreateUser(string userName, string exceptionMessage)
        {
            var message = "Create user failed. UserName: " + userName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorCreateUserException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ChangePassword(string userName)
        {
            var ex = new ChangePasswordException("Password changed. UserName: " + userName);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorChangePassword(string userName, string exceptionMessage)
        {
            var message = "Change password failed. UserName: " + userName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorChangePasswordException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void LockUser(string userName)
        {
            var ex = new LockUserException("User locked out. UserName: " + userName);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorLockUser(string userName, string exceptionMessage)
        {
            var message = "Lock user failed. UserName: " + userName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorLockUserException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void UnLockUser(string userName)
        {
            var ex = new UnLockUserException("User Unlocked. UserName: " + userName);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorUnLockUser(string userName, string exceptionMessage)
        {
            var message = "Unlock user failed. UserName: " + userName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorUnLockUserException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void DeleteUser(string userName)
        {
            var ex = new DeleteUserException("User deleted. UserName: " + userName);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorDeleteUser(string userName, string exceptionMessage)
        {
            var message = "Delete user failed. UserName: " + userName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorDeleteUserException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        #endregion

        #region Authentication

        public static void Authentication(string message)
        {
            var ex = new AuthenticationException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void Login(string userName)
        {
            var ex = new LoginException("User logged in. User name: " + userName);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorLogin(string userName)
        {
            var ex = new ErrorLoginException("Login failed. User name: " + userName);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void Logout(string userName)
        {
            var ex = new LogoutException("User logged out. User name: " + userName);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        #endregion

        #region Authorization

        public static void Authorization(string message)
        {
            var ex = new AuthorizationException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void AuthorizedAccess(string path)
        {
            var ex = new AuthorizedAccessException("Authorized access. Path: " + path);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void UnAuthorizedAccessAttempt(string path)
        {
            var ex = new UnAuthorizedAccessAttemptException("Unauthorized access blocked. Path: " + path);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        #endregion

        #region EntityManipulation

        public static void EntityManipulation(string message)
        {
            var ex = new EntityManipulationException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CreateEntity(string entityTypeName, string entityId)
        {
            var ex = new CreateEntityException("Entity created. EntityType: " + entityTypeName + ", EntityId: " + entityId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorCreateEntity(string entityTypeName, string exceptionMessage)
        {
            var message = "Create entity failed. EntityType: " + entityTypeName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorCreateEntityException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ReadEntity(string entityTypeName, string entityId)
        {
            var ex = new ReadEntityException("Entity readed. EntityType: " + entityTypeName + ", EntityId: " + entityId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorReadEntity(string entityTypeName, string exceptionMessage)
        {
            var message = "Read entity failed. EntityType: " + entityTypeName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorReadEntityException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        //public static void UpdateEntity(string path)
        //{
        //    var ex = new UpdateEntityException("Entity updated. " + path);

        //    Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        //}

        public static void UpdateEntity(string entityTypeName, string entityId)
        {
            var ex = new UpdateEntityException("Entity updated. EntityType: " + entityTypeName + ", EntityId: " + entityId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        //public static void ErrorUpdateEntity(string exceptionMessage)
        //{
        //    var message = "Update entity failed. Exception message: " + exceptionMessage;
        //    var ex = new ErrorUpdateEntityException(message);

        //    Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        //}

        public static void ErrorUpdateEntity(string entityTypeName, string entityId, string exceptionMessage)
        {
            var message = "Update entity failed. EntityType: " + entityTypeName + ", EntityId: " + entityId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorUpdateEntityException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void DeleteEntity(string entityTypeName, string entityId)
        {
            var ex = new DeleteEntityException("Entity deleted. EntityType: " + entityTypeName + ", EntityId: " + entityId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorDeleteEntity(string entityTypeName, string entityId, string exceptionMessage)
        {
            var message = "Delete entity failed. EntityType: " + entityTypeName + ", EntityId: " + entityId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorDeleteEntityException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        #endregion

        #region Communication

        public static void Communication(string message)
        {
            var ex = new CommunicationException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void SendEmail(string from, string to, string subject)
        {
            var ex = new SendEmailException("Email sent. From:" + from + ", To: " + to + ", Subject: " + subject);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorSendEmail(string from, string to, string subject, string exceptionMessage)
        {
            var message = "Send email failed. From:" + from + ", To: " + to + ", Subject: " + subject + ". Exception message: " + exceptionMessage;
            var ex = new ErrorSendEmailException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void SendSms(string from, string to, string message)
        {
            var ex = new SendSmsException("SMS sent. From:" + from + ", To: " + to + ", Message: " + message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorSendSms(string from, string to, string message, string exceptionMessage)
        {
            var msg = "Send SMS failed. From:" + from + ", To: " + to + ", Message: " + message + ". Exception message: " + exceptionMessage;
            var ex = new ErrorSendSmsException(msg);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        #endregion

        #region ApplicationSetting

        public static void ApplicationSetting(string message)
        {
            var ex = new ApplicationSettingException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ChangeApplicationSetting(string settingName, string newValue)
        {
            var ex = new ChangeApplicationSettingException("Application settings changed. SettingName: " + settingName + ", NewValue: " + newValue);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorChangeApplicationSetting(string settingName, string newValue,  string exceptionMessage)
        {
            var message = "Cahnge application settings failed. SettingName:" + settingName + ", NewValue: " + newValue + ". Exception message: " + exceptionMessage;
            var ex = new ErrorChangeApplicationSettingException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        #endregion

        #region SecuritySetting

        public static void SecuritySetting(string message)
        {
            var ex = new SecuritySettingException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CreateRole(string roleName, string roleId)
        {
            var ex = new CreateRoleException("Role created. RoleName: " + roleName + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorCreateRole(string roleName, string exceptionMessage)
        {
            var message = "Create Role failed. RoleName: " + roleName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorCreateRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void UpdateRole(string roleName, string roleId)
        {
            var ex = new UpdateRoleException("Role updated. RoleName: " + roleName + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorUpdateRole(string roleName, string roleId, string exceptionMessage)
        {
            var message = "Update Role failed. RoleName: " + roleName + ", RoleId: " + roleId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorUpdateRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void DeleteRole(string roleName, string roleId)
        {
            var ex = new DeleteRoleException("Role deleted. RoleName: " + roleName + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorDeleteRole(string roleName, string roleId, string exceptionMessage)
        {
            var message = "Delete Role failed. RoleName: " + roleName + ", RoleId: " + roleId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorDeleteRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CreateRoleDetail(string roleDetailName, string roleDetailId)
        {
            var ex = new CreateRoleDetailException("RoleDetail created. RoleDetailName: " + roleDetailName + ", roleDetailId: " + roleDetailId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorCreateRoleDetail(string roleDetailName, string exceptionMessage)
        {
            var message = "Create RoleDetail failed. RoleDetailName: " + roleDetailName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorCreateRoleDetailException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void UpdateRoleDetail(string roleDetailName, string roleDetailId)
        {
            var ex = new UpdateRoleDetailException("RoleDetail updated. RoleDetailName: " + roleDetailName + ", roleDetailId: " + roleDetailId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorUpdateRoleDetail(string roleDetailName, string roleDetailId, string exceptionMessage)
        {
            var message = "Update RoleDetail failed. RoleDetailName: " + roleDetailName + ", roleDetailId: " + roleDetailId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorUpdateRoleDetailException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void DeleteRoleDetail(string roleDetailName, string roleDetailId)
        {
            var ex = new DeleteRoleDetailException("RoleDetail deleted. RoleDetailName: " + roleDetailName + ", roleDetailId: " + roleDetailId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorDeleteRoleDetail(string roleDetailName, string roleDetailId, string exceptionMessage)
        {
            var message = "Delete RoleDetail failed. RoleDetailName: " + roleDetailName + ", roleDetailId: " + roleDetailId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorDeleteRoleDetailException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CreateGroup(string groupName, string groupId)
        {
            var ex = new CreateGroupException("Group created. GroupName: " + groupName + ", groupId: " + groupId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorCreateGroup(string groupName, string exceptionMessage)
        {
            var message = "Create Group failed. GroupName: " + groupName + ", Exception message: " + exceptionMessage;
            var ex = new ErrorCreateGroupException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void UpdateGroup(string groupName, string groupId)
        {
            var ex = new UpdateGroupException("Group updated. GroupName: " + groupName + ", groupId: " + groupId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorUpdateGroup(string groupName, string groupId, string exceptionMessage)
        {
            var message = "Update Group failed. GroupName: " + groupName + ", groupId: " + groupId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorUpdateGroupException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void DeleteGroup(string groupName, string groupId)
        {
            var ex = new DeleteGroupException("Group deleted. GroupName: " + groupName + ", groupId: " + groupId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorDeleteGroup(string groupName, string groupId, string exceptionMessage)
        {
            var message = "Delete Group failed. GroupName: " + groupName + ", groupId: " + groupId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorDeleteGroupException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CreateGroupRole(string groupId, string roleId)
        {
            var ex = new CreateGroupRoleException("GroupRole created. GroupId: " + groupId + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorCreateGroupRole(string groupId, string roleId, string exceptionMessage)
        {
            var message = "Create GroupRole failed. GroupId: " + groupId + ", RoleId: " + roleId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorCreateGroupRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void UpdateGroupRole(string groupId, string roleId)
        {
            var ex = new UpdateGroupRoleException("GroupRole updated. GroupId: " + groupId + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorUpdateGroupRole(string groupId, string roleId, string exceptionMessage)
        {
            var message = "Update GroupRole failed. GroupId: " + groupId + ", RoleId: " + roleId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorUpdateGroupRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void DeleteGroupRole(string groupId, string roleId)
        {
            var ex = new DeleteGroupRoleException("GroupRole deleted. GroupId: " + groupId + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorDeleteGroupRole(string groupId, string roleId, string exceptionMessage)
        {
            var message = "Delete GroupRole failed. GroupId: " + groupId + ", RoleId: " + roleId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorDeleteGroupRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CreateUserGroup(string userId, string groupId)
        {
            var ex = new CreateUserGroupException("UserGroup created. UserId: " + userId + ", GroupId: " + groupId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorCreateUserGroup(string userId, string groupId, string exceptionMessage)
        {
            var message = "Create UserGroup failed. UserId: " + userId + ", GroupId: " + groupId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorCreateUserGroupException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void UpdateUserGroup(string userId, string groupId)
        {
            var ex = new UpdateUserGroupException("UserGroup updated. UserId: " + userId + ", GroupId: " + groupId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorUpdateUserGroup(string userId, string groupId, string exceptionMessage)
        {
            var message = "Update UserGroup failed. UserId: " + userId + ", GroupId: " + groupId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorUpdateUserGroupException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void DeleteUserGroup(string userId, string groupId)
        {
            var ex = new DeleteUserGroupException("UserGroup deleted. UserId: " + userId + ", GroupId: " + groupId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorDeleteUserGroup(string userId, string groupId, string exceptionMessage)
        {
            var message = "Delete UserGroup failed. UserId: " + userId + ", GroupId: " + groupId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorDeleteUserGroupException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CreateUserRole(string userId, string roleId)
        {
            var ex = new CreateUserRoleException("UserRole created. UserId: " + userId + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorCreateUserRole(string userId, string roleId, string exceptionMessage)
        {
            var message = "Create UserRole failed. UserId: " + userId + ", RoleId: " + roleId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorCreateUserRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void UpdateUserRole(string userId, string roleId)
        {
            var ex = new UpdateUserRoleException("UserRole updated. UserId: " + userId + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorUpdateUserRole(string userId, string roleId, string exceptionMessage)
        {
            var message = "Update UserRole failed. UserId: " + userId + ", RoleId: " + roleId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorUpdateUserRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void DeleteUserRole(string userId, string roleId)
        {
            var ex = new DeleteUserRoleException("UserRole deleted. UserId: " + userId + ", RoleId: " + roleId);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void ErrorDeleteUserRole(string userId, string roleId, string exceptionMessage)
        {
            var message = "Delete UserRole failed. UserId: " + userId + ", RoleId: " + roleId + ", Exception message: " + exceptionMessage;
            var ex = new ErrorDeleteUserRoleException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        #endregion

        #region Attacks

        public static void AttackSetting(string message)
        {
            var ex = new AttackException(message);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void SqliAttack(string path)
        {
            var ex = new SqliAttackException("SQLi Attack! " + path);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void XssAttack(string path)
        {
            var ex = new XssAttackException("XSS Attack! " + path);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void IdorAttack(string path)
        {
            var ex = new IdorAttackException("IDOR Attack! " + path);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void CsrfAttack(string path)
        {
            var ex = new CsrfAttackException("CSRF Attack! " + path);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        public static void OverPostingAttack(string path)
        {
            var ex = new OverPostingAttackException("Over Posting Attack! " + path);

            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(ex.ElmahError);
        }

        #endregion
    }
}